<?php

namespace app\admin\controller\auth;

use app\admin\controller\AdminController;
use app\admin\service\auth\RoleService;
use app\admin\service\auth\MenuService;
use app\admin\validate\auth\MenuValidate;
use think\response\Json;

/**
 * 系统菜单权限
 */
class Menu extends AdminController
{

    /**
     * 获取菜单路由
     * @return Json
     */
    public function route()
    {
        $result = MenuService::getMenuByAdminId($this->adminId);
        return $this->data($result);
    }


    /**
     * 获取菜单列表
     * @return Json
     */
    public function lists()
    {
        return $this->data(MenuService::getList());
    }


    /**
     * 菜单详情
     * @return Json
     */
    public function detail()
    {
        $params = (new MenuValidate())->checkParam('detail');
        return $this->data(MenuService::detail($params));
    }


    /**
     * 添加菜单
     * @return Json
     */
    public function add()
    {
        $params = (new MenuValidate())->post()->checkParam('add');
        $result = MenuService::add($params);
        if (true === $result) {
            return $this->success([],'添加成功');
        } else {
            return $this->error(RoleService::getError());
        }
    }


    /**
     * 编辑菜单
     * @return Json
     */
    public function edit()
    {
        $params = (new MenuValidate())->post()->checkParam('edit');
        $result  = MenuService::edit($params);
        if (true === $result) {
            return $this->success([],'编辑成功');
        } else {
            return $this->error(RoleService::getError());
        }
    }


    /**
     * 删除菜单
     * @return Json
     */
    public function delete()
    {
        $params = (new MenuValidate())->delete()->checkParam('delete');
        $result = MenuService::delete($params);
        if (true === $result) {
            return $this->success([],'删除成功');
        } else {
            return $this->error(RoleService::getError());
        }
    }

    /**
     * 获取菜单数据
     * @return Json
     */
    public function all()
    {
        $result = MenuService::getAllData();
        return $this->data($result);
    }

    /**
     * 获取权限数据
     * @return Json
     */
    public function auth()
    {
        $result = MenuService::getAuthByAdminId($this->adminId);
        return $this->data($result);
    }
}